import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:wallet/generated/l10n.dart';
import 'package:wallet/provider/locale_provider.dart';
import 'package:wallet/widget/BaseViewBar.dart';

class LanguagePage extends StatelessWidget {
  static List<Locale> getLocales(BuildContext context) {
    return const <Locale>[
      Locale.fromSubtags(languageCode: 'zh', countryCode: 'CN'),
      Locale.fromSubtags(languageCode: 'ja'),
      Locale.fromSubtags(languageCode: 'en'),
      Locale.fromSubtags(languageCode: 'ko'),
    ];
  }

  static String getText(BuildContext context, int index) {
    if (index == 0) {
      return S
          .of(context)
          .chinese;
    } else if (index == 1) {
      return S
          .of(context)
          .japanese;
    } else if (index == 2) {
      return S
          .of(context)
          .english;
    } else {
      return S
          .of(context)
          .korean;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: BaseViewBar(
          preferredSize: Size.fromHeight(50.0),
          child: BaseTitleBar(
            S
                .of(context)
                .language_settings,
          ),
        ),
        body: ListView.builder(
            itemCount: getLocales(context).length,
            itemBuilder: (context, index) {
              return Container(
                decoration: BoxDecoration(
                    border: Border(
                        bottom: BorderSide(
                            width: 1.0,
                            color: Colors.black12
                        )
                    )
                ),
                child: CheckboxListTile(
                    value: context.read<LocaleProvider>().locale().toString() ==
                        getLocales(context)[index].toString(),
                    title: Text(getText(context, index)),
                    selected:
                    context.read<LocaleProvider>().locale().toString() ==
                        getLocales(context)[index].toString(),
                    onChanged: (value) {
                      context
                          .read<LocaleProvider>()
                          .changeLocale(getLocales(context)[index]);
                    }),
              );
            }));
  }
}
